# 12_jes2001_2016_ideology_ldp_dpj.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-13 Taka-aki Asano
# Last Modified: 2021-10-9

# package
require("dplyr")
require("tidyr")
require("ggplot2")
require("gridExtra")
quartzFonts(HiraKaku = quartzFont(rep('Hiragino Kaku Gothic Pro W3', 4)))
theme_set(theme_classic(base_size = 12, base_family = 'HiraKaku'))


# function to calculate SE
se <- function(x) {
  return(sd(x, na.rm = TRUE) / sqrt(length(x[!is.na(x)])))
}


# LDP
## 2009
## merge
JES2009_Ideology <- JES2009_Respondent[,c("ID", "Ideology")] %>% 
  left_join(JES2009_LDP[,c("ID", "Ideology")], by = "ID")
colnames(JES2009_Ideology) <- c("ID", "Ideology", "LDP")
JES2009_Ideology <- as.data.frame(JES2009_Ideology)

## recode
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 5
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[4] & 
                          JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 4
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[3] & 
                          JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[4]] <- 3
JES2009_Ideology$Position[JES2009_Ideology$Ideology > quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[2] & 
                          JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[3]] <- 2
JES2009_Ideology$Position[JES2009_Ideology$Ideology <= quantile(JES2009_Ideology$Ideology, seq(0, 1, 0.2))[2]] <- 1
mean(JES2009_Ideology$LDP, na.rm = TRUE)
mean(JES2009_Ideology$LDP[JES2009_Ideology$Position == 5], na.rm = TRUE)
mean(JES2009_Ideology$LDP[JES2009_Ideology$Position == 4], na.rm = TRUE)
mean(JES2009_Ideology$LDP[JES2009_Ideology$Position == 3], na.rm = TRUE)
mean(JES2009_Ideology$LDP[JES2009_Ideology$Position == 2], na.rm = TRUE)
mean(JES2009_Ideology$LDP[JES2009_Ideology$Position == 1], na.rm = TRUE)

## 2012
## merge
JES2012_Ideology <- JES2012_Respondent[,c("id", "Ideology")] %>% 
  left_join(JES2012_LDP[,c("id", "Ideology")], by = "id")
colnames(JES2012_Ideology) <- c("id", "Ideology", "LDP")
JES2012_Ideology <- as.data.frame(JES2012_Ideology)

## recode
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 5
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[4] & 
                            JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 4
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[3] & 
                            JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[4]] <- 3
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[2] & 
                            JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[3]] <- 2
JES2012_Ideology$Position[JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[2]] <- 1
mean(JES2012_Ideology$LDP, na.rm = TRUE)
mean(JES2012_Ideology$LDP[JES2012_Ideology$Position == 5], na.rm = TRUE)
mean(JES2012_Ideology$LDP[JES2012_Ideology$Position == 4], na.rm = TRUE)
mean(JES2012_Ideology$LDP[JES2012_Ideology$Position == 3], na.rm = TRUE)
mean(JES2012_Ideology$LDP[JES2012_Ideology$Position == 2], na.rm = TRUE)
mean(JES2012_Ideology$LDP[JES2012_Ideology$Position == 1], na.rm = TRUE)

## plot
LDP_Ideology <- data.frame(Year = c(rep(2009, nrow(JES2009_Ideology)), 
                                    rep(2012, nrow(JES2012_Ideology))), 
                           Position = c(JES2009_Ideology$Position, 
                                        JES2012_Ideology$Position), 
                           LDP = c(JES2009_Ideology$LDP, JES2012_Ideology$LDP))
LDP_Ideology$Year <- as.factor(LDP_Ideology$Year)
LDP_Ideology <- na.omit(LDP_Ideology)
LDP_Ideology_Summary <- aggregate(
  LDP ~ Year + Position, LDP_Ideology, FUN = mean
)
colnames(LDP_Ideology_Summary)[3] <- "Mean"
LDP_Ideology_Summary$SE <- aggregate(
  LDP ~ Year + Position, LDP_Ideology, FUN = se
)[,3]
LDP_Ideology_plot <- ggplot(
  LDP_Ideology_Summary, 
  aes(x = Position, y = Mean, ymin = Mean - 1.96*SE, 
      ymax = Mean + 1.96*SE, shape = Year)) +
  geom_pointrange(position = position_dodge(width = 0.5)) + ylim(-1, 1.5) + 
  labs(x = expression(Left %<-% "Respondents' Ideological Position" %->% Right), 
       y = expression(Left  %<-%  "LDP Position" %->% Right), 
       title = "Recognition of LDP Position", shape = "Year") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(LDP_Ideology_plot)


# DPJ
## 2012
## merge
JES2012_Ideology <- JES2012_Respondent[,c("id", "Ideology")] %>% 
  left_join(JES2012_DPJ[,c("id", "Ideology")], by = "id")
colnames(JES2012_Ideology) <- c("id", "Ideology", "DPJ")
JES2012_Ideology <- as.data.frame(JES2012_Ideology)

## recode
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 5
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[4] & 
                            JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 4
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[3] & 
                            JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[4]] <- 3
JES2012_Ideology$Position[JES2012_Ideology$Ideology > quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[2] & 
                            JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[3]] <- 2
JES2012_Ideology$Position[JES2012_Ideology$Ideology <= quantile(JES2012_Ideology$Ideology, seq(0, 1, 0.2))[2]] <- 1
mean(JES2012_Ideology$DPJ, na.rm = TRUE)
mean(JES2012_Ideology$DPJ[JES2012_Ideology$Position == 5], na.rm = TRUE)
mean(JES2012_Ideology$DPJ[JES2012_Ideology$Position == 4], na.rm = TRUE)
mean(JES2012_Ideology$DPJ[JES2012_Ideology$Position == 3], na.rm = TRUE)
mean(JES2012_Ideology$DPJ[JES2012_Ideology$Position == 2], na.rm = TRUE)
mean(JES2012_Ideology$DPJ[JES2012_Ideology$Position == 1], na.rm = TRUE)


## 2014
## merge
JES2014_Ideology <- JES2014_Respondent[,c("id", "Ideology")] %>% 
  left_join(JES2014_DPJ[,c("id", "Ideology")], by = "id")
colnames(JES2014_Ideology) <- c("id", "Ideology", "DPJ")
JES2014_Ideology <- as.data.frame(JES2014_Ideology)

## recode
JES2014_Ideology$Position[JES2014_Ideology$Ideology > quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 5
JES2014_Ideology$Position[JES2014_Ideology$Ideology > quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[4] & 
                            JES2014_Ideology$Ideology <= quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[5]] <- 4
JES2014_Ideology$Position[JES2014_Ideology$Ideology > quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[3] & 
                            JES2014_Ideology$Ideology <= quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[4]] <- 3
JES2014_Ideology$Position[JES2014_Ideology$Ideology > quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[2] & 
                            JES2014_Ideology$Ideology <= quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[3]] <- 2
JES2014_Ideology$Position[JES2014_Ideology$Ideology <= quantile(JES2014_Ideology$Ideology, seq(0, 1, 0.2))[2]] <- 1
mean(JES2014_Ideology$DPJ, na.rm = TRUE)
mean(JES2014_Ideology$DPJ[JES2014_Ideology$Position == 5], na.rm = TRUE)
mean(JES2014_Ideology$DPJ[JES2014_Ideology$Position == 4], na.rm = TRUE)
mean(JES2014_Ideology$DPJ[JES2014_Ideology$Position == 3], na.rm = TRUE)
mean(JES2014_Ideology$DPJ[JES2014_Ideology$Position == 2], na.rm = TRUE)
mean(JES2014_Ideology$DPJ[JES2014_Ideology$Position == 1], na.rm = TRUE)

## plot
DPJ_Ideology <- data.frame(Year = c(rep(2012, nrow(JES2012_Ideology)), 
                                    rep(2014, nrow(JES2014_Ideology))), 
                           Position = c(JES2012_Ideology$Position, 
                                        JES2014_Ideology$Position), 
                           DPJ = c(JES2012_Ideology$DPJ, JES2014_Ideology$DPJ))
DPJ_Ideology$Year <- as.factor(DPJ_Ideology$Year)
DPJ_Ideology <- na.omit(DPJ_Ideology)

DPJ_Ideology_Summary <- aggregate(
  DPJ ~ Year + Position, DPJ_Ideology, FUN = mean
)
colnames(DPJ_Ideology_Summary)[3] <- "Mean"
DPJ_Ideology_Summary$SE <- aggregate(
  DPJ ~ Year + Position, DPJ_Ideology, FUN = se
)[,3]
DPJ_Ideology_plot <- ggplot(
  DPJ_Ideology_Summary, 
  aes(x = Position, y = Mean, ymin = Mean - 1.96*SE, 
      ymax = Mean + 1.96*SE, shape = Year)) +
  geom_pointrange(position = position_dodge(width = 0.5)) + ylim(-1, 1.5) + 
  labs(x = expression(Left %<-% "Respondents' Ideological Position" %->% Right), 
       y = expression(Left  %<-%  "DPJ Position" %->% Right), 
       title = "Recognition of DPJ Position", shape = "Year") + 
  theme(plot.title = element_text(hjust = 0.5))
plot(DPJ_Ideology_plot)
